package com.ruoyi.common.utils;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.config.EmailConfig;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;

import java.util.HashMap;
import java.util.Map;

@Slf4j
public class FeishuUtils {

    public static void sendFeishu(String name,String title,JSONObject content){
        try{
            JSONObject body = new JSONObject();
            long timestamp = System.currentTimeMillis();
            String sign = "appId=" + EmailConfig.getAppId() + "&apiKey=" + EmailConfig.getApiKey()+ "&timestamp=" + timestamp;
            body.put("sign",SignUtils.encryptBASE64(sign.getBytes()));
            body.put("timestamp",timestamp);

            body.put("name",name);
            body.put("title",title);
            body.put("content",content);
            Map<String,String> map = new HashMap<>();
            map.put("Content-Type","application/json");
            String res = HttpUtils.sendPost(EmailConfig.getSendFeishuUrl(), body.toString(), map);
            log.info("发送飞书返回",res);

        }catch (Exception e){
            log.error("发送飞书异常",e);
        }

    }

    /**
     * sendFeishuToMonit
     * @param apiName
     * @param title
     * @param exception
     */
    public static void sendFeishuToMonit(String apiName,String title,String custNo,String serialNo,Exception exception) {
        String name = "monitor-group";
        JSONObject content = new JSONObject();
        content.put("CustNo", custNo);
        content.put("SerialNo", serialNo);
        content.put("Time", DateUtils.dateTimeNow("yyyy-MM-dd HH:mm:ss"));
        content.put("ClassName", apiName);
        content.put("Error", exception.getMessage());
        sendFeishu(name, title, content);
    }



}
